package com.situ.mall.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.mall.pojo.entity.Category;
import com.situ.mall.pojo.entity.Category;
import com.situ.mall.pojo.query.CategoryQuery;
import com.situ.mall.pojo.vo.CategoryVO;
import com.situ.mall.pojo.vo.CategoryVO;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-09-10
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/selectTopCategoryList")
    public Result<List<CategoryVO>> selectTopCategoryList() {
        List<CategoryVO> list = categoryService.selectTopCategoryList();
        return Result.ok(list);
    }

    @GetMapping("/selectSecondCategoryListByParentId/{id}")
    public Result<List<Category>> selectSecondCategoryListByParentId(@PathVariable Integer id) {
        List<Category> list = categoryService.selectSecondCategoryListByParentId(id);
        return Result.ok(list);
    }

    @GetMapping("/list")
    public Result<IPage<CategoryVO>> list(CategoryQuery categoryQuery) {
        System.out.println("CategoryController.list");
        IPage<CategoryVO> page = categoryService.list(categoryQuery);
        return Result.ok(page);
    }

    // /deleteById?id=1
    // /deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        categoryService.removeById(id);
        return Result.ok("删除成功");
    }

    // /deleteAll/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        categoryService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }


    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回
    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        System.out.println(category);
        categoryService.save(category);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Category category) {
        System.out.println(category);
        //categoryService.saveOrUpdate(category);
        categoryService.updateById(category);
        return Result.ok("更新成功");
    }
}

